package week4;


import jsjf.exceptions.NonComparableElementException;
import week2.LinearNode;

public class LinkedOrderedList<T> extends LinkedList<T> implements OrderedListADT<T> {
    public LinkedOrderedList() {
        super();
    }

    @Override
    public void add(T element) {
        if (!(element instanceof Comparable))
            throw new NonComparableElementException("OrderedList");

        Comparable<T> comparableElement = (Comparable<T>)element;

        LinearNode<T> current = head;
        LinearNode<T> previous = null;
        LinearNode<T> node = new LinearNode<>(element);


        while (( current != null) && (comparableElement.compareTo(current.getElement()) > 0))
        {
            previous = current;
            current = current.getNext();
        }

        if (previous == null )
        {
            head = node;
        }
        else {
            previous.setNext(node);
        }

        node.setNext(current);

        count++;
        modCount++;
    }
}
